<template>
  <div style="display: flex">
    <category-panel title="分类" icon="tree" request="supplierCategory" @delClick="delClick" @notClick="notClick"
                    @nodeClick="nodeClick"></category-panel>
    <el-container>
      <el-header>
        <div class="report_header" ref="headerHeight">
          <div class="report_header_left"></div>
          <div style="width: 300px;display: flex;align-items: center;margin-left: 10px;">
            <svg-icon class="primary" :icon-class="$route.meta.icon"/>
            <span style="margin-left: 5px;" class="primary">{{$route.meta.title}}</span>
          </div>
          <div style="display: flex;flex-direction: row;justify-content: flex-end;width: 100%;">
            <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px;justify-content: flex-end;">
              <div class="report_header_query">
                <el-input prefix-icon="el-icon-search" placeholder="供应商名称"
                          v-model="query.name"></el-input>
              </div>
              <div class="report_header_query">
                <el-input prefix-icon="el-icon-search" placeholder="联系电话"
                          v-model="query.phone"></el-input>
              </div>
            </div>
            <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryClick">查询</el-button>
            <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="addClick"
              v-permission="['supplier.add']">新增</el-button>
          </div>
        </div>
      </el-header>
      <div class="pd10">
        <el-table
          :data="supplierDate"
          border
          stripe
          v-loadMore="[payLoads,'supplier-list']"
          :height="curHeight"
          ref="table"
          style="width: 100%;">
          <el-table-column
            type="index"
            width="50"
            align="center"
            fixed
          >
          </el-table-column>
          <el-table-column
            label="供应商名称"
            align="center"
            prop="name"
          >
          </el-table-column>
          <el-table-column
            label="联系电话"
            width="110"
            align="center"
            prop="mobile"
          >
          </el-table-column>
          <el-table-column
            label="备用电话"
            width="110"
            align="center"
            prop="phone"
          >
          </el-table-column>
          <el-table-column
            label="地址"
            align="center"
          >
            <template slot-scope="scope">
            <span>
              {{ scope.row.areaName }}{{scope.row.address}}
            </span>
            </template>
          </el-table-column>
          <el-table-column
            label="进项税率（%）"
            width="80"
            align="center"
            prop="tax"
          >
          </el-table-column>
          <el-table-column
            prop="taxType"
            label="开票类型"
            align="center"
            width="80"
          >
            <template slot-scope="scope">
              <el-tag type="success">
                <span>{{scope.row.taxType | watchStatus}}</span>
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="邮箱"
            width="180"
            align="center"
            prop="email"
          >
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="80">
            <template slot-scope="scope">
              <div>
                <el-tooltip v-permission="['supplier.edit']" class="item" effect="dark" content="编辑" v-show="deleted == false" placement="top-start">
                  <a @click="editClick(scope.row)">
                    <i style="color: #409EFF" class="el-icon-edit"></i>
                  </a>
                </el-tooltip>
                <el-tooltip class="item" v-permission="['supplier.del']" effect="dark" content="恢复供应商" v-show="deleted == true" placement="top-start">
                  <a style="margin-left: 10px" @click="restoreMchCourse(scope.row)">
                    <i style="color: #E6A23C" class="el-icon-refresh-right"></i>
                  </a>
                </el-tooltip>
                <el-tooltip v-permission="['supplier.del']" class="item" effect="dark" content="删除" v-show="deleted == false" placement="top-start">
                  <a style="margin-left: 10px" @click="deleteSupplier(scope.row)">
                    <i style="color: #F56C6C" class="el-icon-delete"></i>
                  </a>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-footer class="el-footerBox">
        <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="loadSupplier"></pagination>
      </el-footer>
    </el-container>
  </div>
</template>

<script>
  import permission from '@/directive/permission/index.js' // 权限判断指令
  import {list, del ,update} from '@/api/supplier'
  import utils from '../../utils/utils.js'
  import {EventBus} from '@/api/eventBus'
  import Pagination from '@/components/Pagination';
  import CategoryPanel from '@/components/CategoryPanel'
  import loadMore from '@/utils/loadMore.js'
  export default {
    name: 'supplier-list',
    directives: { permission,loadMore },
    activated(){
      this.payLoads = new Date().getTime();
    },
    data() {
      return {
        supplierDate: [],
        pageNum: 1,
        payLoads:'',
        pageSize: 20,
        total: 0,
        queryType: true,
        query: {
          name: '',
          phone: ''
        },
        keyword: {
          name: '',
          phone: ''
        },
        queryPhone: '',
        queryName: '',
        curHeight: null,
        excelData: [],
        supplierCategoryId: '',
        deleted: false,
        restoreForm: {
          id: '',
          deleted: false,
        },
      }
    },
    components: {Pagination, CategoryPanel},
    created() {
      var _this = this
      this.loadSupplier()
      EventBus.$on('supplier-add', res => { // 监听新增完毕
        // 更新列表数组
        _this.supplierDate.splice(0, 0, res)
      });
      EventBus.$on('supplier-edit', res => { // 监听新增完毕
        // 更新列表数组
        for (var i = 0; i < _this.supplierDate.length; i++) {
          if (_this.supplierDate[i].id == res.id) {
            _this.supplierDate[i] = res;
          }
        }
      })
    },
    mounted() {
      this.comeIn()
      this.beforeMount()
    },
    filters: {
      formatDate: function (value) {
        let date = new Date(value);
        let y = date.getFullYear();
        let MM = date.getMonth() + 1;
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + MM + '-' + d;
      },
      watchStatus(val) {
        if (val == 1) {
          return '增票'
        } else if (val == 2) {
          return '普票'
        } else {
          return '不开票'
        }
      },
    },
    methods: {
      beforeMount() {
        window.onresize = () => {
          return (() => {
            if (this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              this.curHeight = h - 266//减去页面上固定高度height
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              this.curHeight = h - 220//减去页面上固定高度height
            }
          })();
        };
      },
      comeIn(){
        console.log('222')
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 220//减去页面上固定高度height
      },
      editClick(row) {
        this.$router.push({
          path: '/supplier/edit', query: {id: row.id}
        })
      },
      loadSupplier(val) {
        console.log(val)
        var _this = this
        let params = new URLSearchParams();
        params.append('pageNum', _this.pageNum);
        params.append('pageSize', _this.pageSize);
        params.append('phone', _this.keyword.phone);
        params.append('name', _this.keyword.name);
        params.append('supplierCategoryId', _this.supplierCategoryId);
        params.append('deleted', _this.deleted);
        list(params).then(response => {
          console.log(response)
          _this.supplierDate = response.data.data
          _this.total = response.data.total
        }).catch(error => {
          console.log('error!!')
        });
      },

      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 266//减去页面上固定高度height
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 220//减去页面上固定高度height
        }
      },
      addClick() {
        this.$router.push({
          path: '/supplier/add'
        })
      },
      deleteSupplier(data) {
        console.log(data)
        var _this = this
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          del(data.id).then(response => {
            console.log(response)
            _this.supplierDate.forEach(function (item,index) {
              if (item.id == data.id){
                _this.supplierDate.splice(index,1)
              }
            });
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            console.log('error!!')
          });
        }).catch(() => {
        });
      },
      queryClick() {
        this.keyword = this.query;
        this.pageNum = 1
        this.loadSupplier()
      },
      resetClick() {
        this.query.phone = '';
        this.query.name = '';
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadSupplier()
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
      notClick() {
        this.supplierCategoryId = 0;
        this.deleted = false;
        this.loadSupplier()
      },
      delClick() {
        this.deleted = true;
        this.supplierCategoryId = '';
        this.loadSupplier()
      },
      nodeClick(data) {
        this.deleted = false;
        this.supplierCategoryId = data.id
        this.loadSupplier()
      },
      restoreMchCourse(row) {
        var _this = this
        console.log(row);
        this.$confirm('确认恢复?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.restoreForm.id = row.id;
          update(_this.restoreForm).then(response => {
            console.log(response);
            _this.supplierDate.forEach(function(item, index) {
              if (item.id == row.id) {
                _this.supplierDate.splice(index, 1)
              }
            })
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            console.log('error!!')
          });
        }).catch(() => {
        });
      },
    }
  }
</script>

<style scoped>
  .el-footerBox {
    height: 50px !important;
    line-height: 50px !important;
    border-top: 1px solid #f2f2f2;
  }
  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }

  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
  .report_header_date {
      margin-left: 10px;
      width: 250px;
      margin-top: 5px;
    }
</style>
